package com.gdssoft.pdaweb.model;

import java.util.Calendar;
import java.util.Date;

import com.gdssoft.core.model.BaseModel;
import com.gdssoft.core.model.CheckCompleteResult;
import com.gdssoft.core.model.GenericModel;
import com.google.gson.annotations.Expose;

/**
 * 电表抄表月计划
 * @author F3230313
 *
 */
public class AutoPlan extends BaseModel implements GenericModel {
	private static final long serialVersionUID = 1L;

	public static final Integer TYPE_AM = 1;
	public static final Integer TYPE_WATER = 2;
	
	public static final Integer STATUS_OPEN = 0;
	public static final Integer STATUS_CLOSE = 1;
	
	@Expose
	protected Long id;
	@Expose
	protected Integer type;
	@Expose
	protected Long pId;
	@Expose
	protected Date executeDate;
	@Expose
	protected Date beginTime;
	@Expose
	protected Date endTime;
	@Expose
	protected String executor;
	@Expose
	protected Integer status;
	@Expose
	protected String lastEditBy;
	@Expose
	protected Date lastEditDt;
	@Expose
	protected String createdby;
	@Expose
	protected Date createddt;
	
	public AutoPlan() {
		
	}
	public AutoPlan(Long planId, Integer type, Date beginTime, Date endTime, String executor, String createBy) {
		this.pId = planId;
		this.beginTime = beginTime;
		this.endTime = endTime;
		this.executor = executor;
		this.executeDate = Calendar.getInstance().getTime();
		this.createddt = this.executeDate;
		this.type = type;
		this.status = AutoPlan.STATUS_OPEN;
		this.createdby = createBy;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Long getpId() {
		return pId;
	}

	public void setpId(Long pId) {
		this.pId = pId;
	}
	
	public Date getExecuteDate() {
		return executeDate;
	}

	public void setExecuteDate(Date executeDate) {
		this.executeDate = executeDate;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getExecutor() {
		return executor;
	}

	public void setExecutor(String executor) {
		this.executor = executor;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getLastEditBy() {
		return lastEditBy;
	}

	public void setLastEditBy(String lastEditBy) {
		this.lastEditBy = lastEditBy;
	}

	public Date getLastEditDt() {
		return lastEditDt;
	}

	public void setLastEditDt(Date lastEditDt) {
		this.lastEditDt = lastEditDt;
	}

	public String getCreatedby() {
		return createdby;
	}

	public void setCreatedby(String createdby) {
		this.createdby = createdby;
	}

	public Date getCreateddt() {
		return createddt;
	}

	public void setCreateddt(Date createddt) {
		this.createddt = createddt;
	}
	
	public void setAutoPlanClosed() {
		this.status = AutoPlan.STATUS_CLOSE;
	}
	
	/**
	 * 构造下一个月的自动执行实例
	 * pId、type、executor 复制当前值
	 * executeDate、beginTime、endTime 各增加 1 个月
	 * @param createby 创建人
	 * @return 下一个月的自动执行实例
	 */
	public AutoPlan nextMonthAutoPlan(String createby) {
		//如果当前实例不完整无法执行构造动作（未发育怎么Make下一代）
		if (!this.checkComplete().isComplete()) {
			return null;
		}
		
		AutoPlan nextOne = new AutoPlan();
		nextOne.setpId(this.pId);
		nextOne.setType(this.type);
		
		Calendar cal = Calendar.getInstance();  
		
		cal.setTime(this.executeDate);
		cal.add(Calendar.MONTH, 1);
		nextOne.setExecuteDate(cal.getTime());
		
		cal.setTime(this.beginTime);
		cal.add(Calendar.MONTH, 1);
		nextOne.setBeginTime(cal.getTime());
		
		cal.setTime(this.endTime);
		cal.add(Calendar.MONTH, 1);
		nextOne.setEndTime(cal.getTime());
		
		nextOne.setExecutor(this.executor);
		nextOne.setStatus(AutoPlan.STATUS_OPEN);
		nextOne.setCreatedby(createby);
		nextOne.setCreateddt(Calendar.getInstance().getTime());
		return nextOne;
	}
	
	@Override
	public CheckCompleteResult checkComplete() {
		CheckCompleteResult result = new CheckCompleteResult();
		if (this.type != AutoPlan.TYPE_AM && this.type != AutoPlan.TYPE_WATER) {
			result.setIsComplete(false);
			result.setMessage("Type 的值只能是 " + AutoPlan.TYPE_AM.toString() 
					+ " 或 " + AutoPlan.TYPE_WATER.toString());
		}
		if (this.pId == null) {
			result.setIsComplete(false);
			result.setMessage("计划ID不允许为空");
		}
		if (this.executeDate == null) {
			result.setIsComplete(false);
			result.setMessage("计划的执行日期不允许为空");
		}
		if (this.beginTime == null) {
			result.setIsComplete(false);
			result.setMessage("计划的开始时间不允许为空");
		}
		if (this.endTime == null) {
			result.setIsComplete(false);
			result.setMessage("计划的结束时间不允许为空");
		}
		if (this.executor == null) {
			result.setIsComplete(false);
			result.setMessage("计划的执行人不允许为空");
		}
		if (this.id == null && this.createdby == null) {
			result.setIsComplete(false);
			result.setMessage("新的实例必须要有 createdby ");
		}
		return result;
	}

}
